import re


def convert(self, value):
    matched = value.group()
    number = int(matched[13:-1])
    return 'sParameter%d' % (number+1)


class Convert():
    def convert(self, value):
        matched = value.group()
        number = int(matched[13:-1])
        return 'sParameter%d' % (number + 1)

# 替换文本中数组改为改为变量名+数组序号+1
def convert_demo():
    c = Convert()
    with open('./doc/new.txt', "a") as file_to_write:
        with open("./doc/old.txt", "r", encoding='utf-8') as file_to_read:
            while True:
                line_content = file_to_read.readline()  # 整行读取数据
                if not line_content:
                    break
                    pass
                line_content = str.replace(line_content, "\n", "")
                # 字符串s中数字大于等于6的替换为x 小于6的替换为k
                # r = re.sub('\d', c.convert, Convert.s)
                regex_text = re.sub('contractInfo\[\d{1,}\]', c.convert, line_content)
                try:
                    file_to_write.write(regex_text + '\n')
                except UnicodeEncodeError:
                    print('%d#%s' % (regex_text, '转码异常#########################################'))
                pass
            pass

if __name__ == '__main__':
    convert_demo()